import { getHost } from '@/utils';

const state = {
  ossDomain: ''
},

mutationTypes = {
  SET_USER: 'setUser'
},

actionTypes = {
  SET_USER: 'setUser'
},

mutations = {
  [mutationTypes.SET_USER] (state, payload = {}) {
    Object.keys(state).forEach(key => {
      payload[key] !== undefined && (state[key] = (payload || {})[key]);
    });
    localStorage.setItem('loginData', JSON.stringify(payload));
  }
},

actions = {
  async [actionTypes.SET_USER] ({ commit, state }) {
    const protocal = `${window.location.protocol}//${getHost()}`;
    commit(mutationTypes.SET_USER, {
      ossDomain: protocal
    });
    return protocal;
  }
};

export default {
  state,
  mutations,
  actions
};
